package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Integer> findRoleIdsByMenuId(Integer id) {
        List<Integer> list = roleMapper.findRoleIdsByMenuId(id);
        return list;
    }

    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.findAll();
        return list;
    }


    @Override
    public void add(Role role) {
        roleMapper.add(role);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义当前页的页码和页面大小
        PageHelper.startPage(currentPage,pageSize);
        //条件查询数据
        List<Role> list = roleMapper.findCountByCondition(queryString);

        PageInfo<Role> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void edit(Role role) {
        roleMapper.edit(role);
    }

    @Override
    public void deleteById(Integer id) {
        Integer count =roleMapper.findCountByRoleId(id);
        if (count>0){
            //抛出异常,提示不能删除
            throw new RuntimeException(MessageConstant.ROLE_IS_QUOTED);
        }
        //删除检查项
        roleMapper.deleteById(id);
    }
}
